home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 17.8 KB | 611 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWShpLst.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWSHPLST_H
- #include "FWShpLst.h"
- #endif
-
- #ifndef FWSHAPE_H
- #include "FWShape.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWGraphx_ShapeList
- #endif
-
- FW_DEFINE_CLASS_M1(FW_PShapeList, FW_CGraphicCountedPtr)
- FW_DEFINE_CLASS_M1(FW_CShapeListRep, FW_CGraphicCountedPtrRep)
- FW_REGISTER_ARCHIVABLE_CLASS(FW_LShapeList, FW_CShapeListRep, FW_CShapeListRep::Read, FW_CGraphicCountedPtrRep::Write)
-
- FW_DEFINE_CLASS_M0(FW_CShapeListIterator)
-
- //========================================================================================
- // class FW_PShapeList
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::FW_PShapeList()
- {
- SetRep(new FW_CShapeListRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::FW_PShapeList(FW_CShapeListRep* rep)
- {
- SetRep(rep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::FW_PShapeList(FW_CReadableStream& archive)
- {
- SetRep(new FW_CShapeListRep(archive));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::~FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::~FW_PShapeList()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::FW_PShapeList
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList::FW_PShapeList(const FW_PShapeList& other) :
- FW_CGraphicCountedPtr(other)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PShapeList::operator=
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList& FW_PShapeList::operator=(const FW_PShapeList& other)
- {
- // We don't need to test this == &other because SetRep will do it
- SetRep(other.GetRep());
- return *this;
- }
-
- //========================================================================================
- // class FW_CPrivShapeListNode
- //========================================================================================
-
- class FW_CPrivShapeListNode
- {
- public:
- FW_CPrivShapeListNode();
- FW_CPrivShapeListNode(FW_CPrivShapeListNode* prev, FW_CShape* shape);
- ~FW_CPrivShapeListNode();
-
- FW_CPrivShapeListNode* fPrev;
- FW_CPrivShapeListNode* fNext;
-
- FW_CShape* fShape;
- };
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivShapeListNode::FW_CPrivShapeListNode
- //----------------------------------------------------------------------------------------
-
- FW_CPrivShapeListNode::FW_CPrivShapeListNode() :
- fPrev(NULL),
- fNext(NULL),
- fShape(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivShapeListNode::FW_CPrivShapeListNode
- //----------------------------------------------------------------------------------------
-
- FW_CPrivShapeListNode::FW_CPrivShapeListNode(FW_CPrivShapeListNode* prev, FW_CShape* shape) :
- fPrev(prev),
- fNext(prev->fNext),
- fShape(shape)
- {
- fPrev->fNext = this;
- fNext->fPrev = this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivShapeListNode::~FW_CPrivShapeListNode
- //----------------------------------------------------------------------------------------
-
- FW_CPrivShapeListNode::~FW_CPrivShapeListNode()
- {
- if(fPrev != NULL)
- fPrev->fNext = fNext;
-
- if(fNext != NULL)
- fNext->fPrev = fPrev;
- }
-
- //========================================================================================
- // class FW_CShapeListRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::FW_CShapeListRep() :
- fHead(NULL),
- fTail(NULL),
- fCount(0)
- {
- fHead = new FW_CPrivShapeListNode;
- fTail = new FW_CPrivShapeListNode;
-
- fHead->fNext = fTail;
- fTail->fPrev = fHead;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::FW_CShapeListRep(const FW_CShapeListRep& other) :
- fHead(NULL),
- fTail(NULL),
- fCount(0)
- {
- fHead = new FW_CPrivShapeListNode;
- fTail = new FW_CPrivShapeListNode;
-
- fHead->fNext = fTail;
- fTail->fPrev = fHead;
-
- FW_CShapeListIterator ite(FW_CShapeListIterator::kFrontToBack, &other);
- FW_CShape* shape;
- while((shape = ite.Next()) != NULL)
- AdoptAtBack(shape->Copy());
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::~FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::~FW_CShapeListRep()
- {
- RemoveAll();
-
- delete fHead;
- delete fTail;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::GetCount
- //----------------------------------------------------------------------------------------
-
- unsigned long FW_CShapeListRep::GetCount() const
- {
- return fCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Purge
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::Purge()
- {
- FW_CShape* shape;
- FW_CShapeListIterator ite(FW_CShapeListIterator::kFrontToBack, this);
- while((shape = ite.Next()) != NULL)
- shape->Purge();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptAtFront
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptAtFront(FW_CShape* shape)
- {
- new FW_CPrivShapeListNode(fTail->fPrev, shape);
- ++ fCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptAtBack
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptAtBack(FW_CShape* shape)
- {
- new FW_CPrivShapeListNode(fHead, shape);
- ++ fCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptAfter
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptAfter(FW_CShape* shapeToAdd, FW_CShape* afterWhich)
- {
- FW_CPrivShapeListNode* node = FindNode(afterWhich);
- FW_ASSERT(node != NULL);
- new FW_CPrivShapeListNode(node->fPrev, shapeToAdd);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::AdoptBefore
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::AdoptBefore(FW_CShape* shapeToAdd, FW_CShape* beforeWhich)
- {
- FW_CPrivShapeListNode* node = FindNode(beforeWhich);
- FW_ASSERT(node != NULL);
- new FW_CPrivShapeListNode(node, shapeToAdd);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Contains
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::Contains(FW_CShape* shape) const
- {
- return FindNode(shape) != NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Remove
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::Remove(FW_CShape* shape)
- {
- FW_CPrivShapeListNode* node = FindNode(shape);
- FW_ASSERT(node != NULL);
- delete node;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::RemoveAll
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::RemoveAll()
- {
- while(fCount != 0)
- {
- delete fHead->fNext->fShape;
- delete fHead->fNext;
- -- fCount;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::RemoveTop
- //----------------------------------------------------------------------------------------
-
- FW_CShape* FW_CShapeListRep::RemoveTop()
- {
- FW_ASSERT(fCount != 0);
- FW_CShape* shape = fTail->fPrev->fShape;
- delete fTail->fPrev;
- return shape;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::RemoveBottom
- //----------------------------------------------------------------------------------------
-
- FW_CShape* FW_CShapeListRep::RemoveBottom()
- {
- FW_ASSERT(fCount != 0);
- FW_CShape* shape = fHead->fNext->fShape;
- delete fHead->fNext;
- return shape;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveForward
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveForward(FW_CShape* shape)
- {
- FW_CPrivShapeListNode* node = FindNode(shape);
- FW_ASSERT(node != NULL);
-
- if(node->fNext != fTail)
- {
- new FW_CPrivShapeListNode(node->fNext, shape);
- delete node;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveToFront
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveToFront(FW_CShape* shape)
- {
- FW_CPrivShapeListNode* node = FindNode(shape);
- FW_ASSERT(node != NULL);
-
- if(node->fNext != fTail)
- {
- new FW_CPrivShapeListNode(fTail->fPrev, shape);
- delete node;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveBack
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveBack(FW_CShape* shape)
- {
- FW_CPrivShapeListNode* node = FindNode(shape);
- FW_ASSERT(node != NULL);
-
- if(node->fPrev != fHead)
- {
- new FW_CPrivShapeListNode(node->fPrev->fPrev, shape);
- delete node;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::MoveToBack
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::MoveToBack(FW_CShape* shape)
- {
- FW_CPrivShapeListNode* node = FindNode(shape);
- FW_ASSERT(node != NULL);
-
- if(node->fPrev != fHead)
- {
- new FW_CPrivShapeListNode(fHead, shape);
- delete node;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::FindNode
- //----------------------------------------------------------------------------------------
-
- FW_CPrivShapeListNode* FW_CShapeListRep::FindNode(FW_CShape* shape) const
- {
- for(FW_CPrivShapeListNode* node = fHead->fNext; node != fTail; node = node->fNext)
- {
- if(node->fShape == shape)
- return node;
- }
-
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::FW_CShapeListRep
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListRep::FW_CShapeListRep(FW_CReadableStream& archive) :
- fHead(NULL),
- fTail(NULL),
- fCount(0)
- {
- fHead = new FW_CPrivShapeListNode;
- fTail = new FW_CPrivShapeListNode;
-
- fHead->fNext = fTail;
- fTail->fPrev = fHead;
-
- // Read and add the shapes
- unsigned long count;
- archive >> count;
-
- while(count -- != 0)
- {
- FW_CShape* shape;
- FW_READ_DYNAMIC_OBJECT(archive, &shape, FW_CShape);
-
- AdoptAtBack(shape);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListRep::Flatten(FW_CWritableStream& archive) const
- {
- archive << fCount;
-
- FW_CShapeListIterator ite(FW_CShapeListIterator::kFrontToBack, this);
- FW_CShape* shape;
- while((shape = ite.Next()) != NULL)
- FW_WRITE_DYNAMIC_OBJECT(archive, shape, FW_CShape);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Read
- //----------------------------------------------------------------------------------------
-
- void* FW_CShapeListRep::Read(FW_CReadableStream& archive)
- {
- FW_CShapeListRep* shapeList = new FW_CShapeListRep(archive);
- return shapeList;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListRep::IsEqual(const FW_CGraphicCountedPtrRep* other) const
- {
- if (other == this)
- return TRUE;
-
- FW_CShapeListRep* rep = FW_DYNAMIC_CAST(FW_CShapeListRep, other);
- if (rep != NULL)
- {
- if(fCount == rep->fCount)
- {
- FW_CShapeListIterator iteThis(FW_CShapeListIterator::kFrontToBack, this);
- FW_CShapeListIterator iteThat(FW_CShapeListIterator::kFrontToBack, rep);
-
- for (unsigned long i = 0; i< fCount; ++ i)
- {
- FW_CShape* shapeThis = iteThis.Next();
- FW_CShape* shapeThat = iteThat.Next();
-
- if(shapeThis != shapeThat)
- return FALSE;
- }
- return TRUE;
- }
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListRep::Copy
- //----------------------------------------------------------------------------------------
-
- FW_PShapeList FW_CShapeListRep::Copy() const
- {
- return FW_PShapeList(new FW_CShapeListRep(*this));
- }
-
- //========================================================================================
- // class FW_CShapeListIterator
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListIterator::FW_CShapeListIterator
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListIterator::FW_CShapeListIterator(EDirection direction, const FW_PShapeList& shapeList)
- {
- CommonInit(direction, shapeList->fHead, shapeList->fTail);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListIterator::FW_CShapeListIterator
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListIterator::FW_CShapeListIterator(EDirection direction, const FW_CShapeListRep* rep)
- {
- CommonInit(direction, rep->fHead, rep->fTail);
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListIterator::CommonInit
- //----------------------------------------------------------------------------------------
-
- void FW_CShapeListIterator::CommonInit(EDirection direction,
- FW_CPrivShapeListNode* head,
- FW_CPrivShapeListNode* tail)
- {
- fDirection = direction;
-
- if(fDirection == kFrontToBack)
- {
- fFirst = tail->fPrev;
- fPastLast = head;
- }
- else
- {
- fFirst = head->fNext;
- fPastLast = tail;
- }
-
- fCurrent = fFirst;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListIterator::~FW_CShapeListIterator
- //----------------------------------------------------------------------------------------
-
- FW_CShapeListIterator::~FW_CShapeListIterator()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListIterator::First
- //----------------------------------------------------------------------------------------
-
- FW_CShape* FW_CShapeListIterator::First()
- {
- if(fFirst != fPastLast)
- {
- fCurrent = fFirst;
- return Next();
- }
-
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListIterator::Next
- //----------------------------------------------------------------------------------------
-
- FW_CShape* FW_CShapeListIterator::Next()
- {
- if(fCurrent != fPastLast)
- {
- FW_CShape* shape = fCurrent->fShape;
-
- if(fDirection == kFrontToBack)
- fCurrent = fCurrent->fPrev;
- else
- fCurrent = fCurrent->fNext;
-
- return shape;
- }
-
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CShapeListIterator::IsNotComplete
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CShapeListIterator::IsNotComplete() const
- {
- return fCurrent != fPastLast;
- }
-
-